package day0411;

import java.util.Scanner;

public class Test05 {
    public static void main(String[] args) {
        int[] arr = {8,4,2,1,23,344,12};
        int n = arr.length;
        boolean swapped;
        for (int i = 0; i < n; i++) {
            swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swapped = true;
                }
            }
            for (int a : arr) {
                System.out.print(a + " ");
            }
            System.out.println();
            if (!swapped) {
                break;
            }
        }
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入想要查找的数字: ");
        int target = sc.nextInt();
        int left = 0;
        int right = arr.length - 1;
        int result = -1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                result = mid;
                break;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        if (result != -1) {
            System.out.println("目标元素 " + target + " 在数组中的索引是: " + result);
        } else {
            System.out.println("目标元素 " + target + " 不在数组中。");
        }
    }
}
